package com.jumper.study.admin.service.impl;

import java.util.List;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jumper.study.admin.service.AdminService;
import com.jumper.study.db.dao.AdminMapper;
import com.jumper.study.db.model.Admin;

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
    //根据用户账号id查找
    @Cacheable(value="admin_user", key="#accountId", unless="#result == null")
    public Admin getByAccountId(Long accountId) {
        LambdaQueryWrapper<Admin> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Admin::getAccountId, accountId);
        List<Admin> adminList = getBaseMapper().selectList(wrapper);
        if (adminList != null && adminList.size() == 1) {
            return adminList.get(0);
        }
        return null;
    }

    //读取联全账号信息列表
    public List<Admin> getWithAccountList() {
        return getBaseMapper().selectWithAccountList();
    }

    @Override
    public boolean disableById(Admin entity) {
        if (entity.isEnabled()) {
            entity.setDisabled(1);
        } else {
            entity.setDisabled(0);
        }

        return updateById(entity);
    }
}
